package com.cy.java.thread;


import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicLong;

public class ThreadExecutorPoolTests {
    public static void main(String[] args) {
        ThreadFactory threadFactory=new ThreadFactory() {
            private AtomicLong al=new AtomicLong(1);
            @Override
            public Thread newThread(Runnable r) {
                return new Thread(r,"cgb2007-thread-"+al.getAndIncrement());
            }
        };
        BlockingQueue<Runnable> workQueue=new ArrayBlockingQueue<>(3);
        ThreadPoolExecutor threadPoolExecutor=
                new ThreadPoolExecutor(2,3,60,
                        TimeUnit.SECONDS,workQueue,threadFactory);
        threadPoolExecutor.execute(new Runnable() {
            @Override
            public void run() {
                String tName=Thread.currentThread().getName();
                System.out.println(tName+"->task-01");
                try{Thread.sleep(3000);}catch (Exception e){}
            }
        });
        threadPoolExecutor.execute(new Runnable() {
            @Override
            public void run() {
                String tName=Thread.currentThread().getName();
                System.out.println(tName+"->task-02");
                try{Thread.sleep(3000);}catch (Exception e){}
            }
        });
        threadPoolExecutor.execute(new Runnable() {
            @Override
            public void run() {
                String tName=Thread.currentThread().getName();
                System.out.println(tName+"->task-03");
                try{Thread.sleep(3000);}catch (Exception e){}
            }
        });
    }
}
